import json
import random
import re
import urllib
import web

def getWikimages(query):
    wikimediaAPIURL = "http://commons.wikimedia.org/w/api.php?"
    params = urllib.urlencode({'action':'query',
                              'titles':query,
                              'prop':'images',
                              'imlimit':20,
                              'format':'json'})
    req = urllib.urlopen(wikimediaAPIURL + params + '&redirects')
    res = req.read()
    wikiResJSON = json.loads(res)
    wikiResults = []
    result = wikiResJSON.get('query');
    pages = wikiResJSON.get("query").get("pages");
    images = []
    imagesURLs = []
    for item in pages:
        imageList = pages.get(item).get("images")
        if imageList:
            for image in imageList:
                images.append(image.get('title'))
        else:
            continue
    
    if len(images) < 5:
        params = urllib.urlencode({'action':'query',
                                   'list':'allimages',
                                   'ailimit':10,
                                   'aifrom':query,    
                                   'aiprop':'dimensions|mime',
                                   'format':'json'})
        req = urllib.urlopen(wikimediaAPIURL + params + '&redirects')
        res = req.read()
        wikiResJSON = json.loads(res)
        wikiResults = []
        result = wikiResJSON.get('query')
        allimages = result.get('allimages')
        images = []
        imagesURLs = []
        for image in range(len(allimages)):
            images.append(allimages[image].get('title'))
    for image in images:
        iParams = urllib.urlencode({'action':'query',
                                    'titles':image.encode('uft8'),
                                    'prop':'imageinfo',
                                    'iiprop':'url',
                                    'format':'json'})
        iReq = urllib.urlopen(wikimediaAPIURL + iParams)
        iRes = iReq.read()
        iResJSON = json.loads(iRes)
        imageResults = []
        iResult = iResJSON.get("query");
        pages = iResJSON.get("query").get("pages");
        #print pages
        for item in pages:
            imageUrlInfo = pages.get(item).get("imageinfo")
            if imageUrlInfo == None:
                continue
            for entries in imageUrlInfo:
                iURL = entries.get("url")
                imagesURLs.append(iURL)
    return imagesURLs    

def getWikipedia(query):
    wikipediaAPIURL = "http://en.wikipedia.org/w/api.php?"
    params = urllib.urlencode({'format':'json',
                               'action':'opensearch',
                               'search':query,
                               'limit':20})
    req = urllib.urlopen(wikipediaAPIURL + params)
    res = req.read()
    wikiResJSON = json.loads(res)
    wikiResults = []
    if (len(wikiResJSON[1]) == 0):
        wikiDonate = "https://donate.wikimedia.org/"
        articleObj = {'title': query + ' - Wikipedia, the free encyclopedia', 
                             'url':('http://www.wikipedia.org/wiki/' + query), 
                             'kwic': ' Wikipedia does not have an article with this exact name. Donate to help wikipedia - ' + 
                             "<a href='" + wikiDonate + "'>" + "donate" + "<a/>"}
        wikiResults.append(articleObj)
        return wikiResults
    
    for article in wikiResJSON[1]:
        extractParams = urllib.urlencode({'action':'query',
                                               'format':'json',
                                               'prop':'extracts',
                                               'exchars':350,
                                               'exintro':'true',
                                               'exsectionformat':'plain',
                                               'smlimit':1,
                                               'titles':article.encode('utf8'),
                                               })
        articleReq = urllib.urlopen(wikipediaAPIURL + extractParams + '&redirects')
        articleRes = articleReq.read()
        articleJson = json.loads(articleRes)
        exArticlesJson = articleJson.get('query').get('pages')
        extract = ''
        for exArticle in exArticlesJson:
                extract = exArticlesJson.get(exArticle).get('extract')
                extract= re.sub('<[^<]+?>','',extract)
                break;
        articleUrl = article.replace(' ','_')
        articleObj = {'title': article + ' - Wikipedia, the free encyclopedia', 
                             'url':('http://www.wikipedia.org/wiki/' + articleUrl), 
                             'kwic':extract}
        wikiResults.append(articleObj)
    return wikiResults

urls = (
        '/', 'index',
        '/search', 'search')

class index:
    def GET(self):
        render = web.template.render('templates/')
        return render.index()

class search:
    def GET(self):
        try:
            query = web.input()
            keyword = query.query.encode('utf8')
            farooParams = urllib.urlencode({'q':keyword, 'f':'json', 'i':'false', 'length':200, 'src':'web'})
            f = urllib.urlopen("http://www.faroo.com/api?" + farooParams)
            msg = f.read()
            result = json.loads(msg)
            results = result.get('results')
            for item in results:
                if re.search('wikipedia', item.get('url'),re.IGNORECASE) is not None:
                    results.remove(item)
                
                if len(results) <6:
                    articleObj = {'title': keyword + ' is not well documented on the Internet',
                                'url': ("https://donate.wikimedia.org/"),
                                'kwic' : 'Support Wikipedia in an effort to document ' + keyword
                             }
                    results.insert(0,articleObj)
                elif len(results) <10:
                    articleObj = {'title': 'A better way to look for "' + keyword + '"?',
                              'url': ("https://donate.wikimedia.org/"),
                              'kwic' : 'Wikipedia has the technology. Be part of our cause. '
                              }
                    results.insert(3,articleObj)
                
                elif len(results) >9:
                    articleObj = {'title': 'Still have not found "' + keyword + '"?',
                              'url': ("https://donate.wikimedia.org/"),
                              'kwic' : 'Wikipedia will get on it! We just need a small favor from you.'
                              }
                    results.append(articleObj)
            newWikiResults = getWikipedia(keyword)
            nextIndex = 0
            for wiki in newWikiResults:
                results.insert(nextIndex,wiki)
                nextIndex += random.randrange(2,10)            
            
            for item in results:
                wikiDonate = "https://donate.wikimedia.org/"
                if re.search('charity', item.get('kwic'),re.IGNORECASE) is not None:
                    newKwic = item.get('kwic').replace('charity', "<i><a href='" + wikiDonate + "'> charity <a/></i>" )
                elif re.search('support', item.get('kwic'),re.IGNORECASE) is not None:
                    newKwic = item.get('kwic').replace('support', "<i><a href='" + wikiDonate + "'> support <a/></i>" )
                elif re.search('donation', item.get('kwic'),re.IGNORECASE) is not None:
                    newKwic = item.get('kwic').replace('donation', "<i><a href='" + wikiDonate + "'> donation <a/></i>" )
                elif re.search('help', item.get('kwic'),re.IGNORECASE) is not None:
                    newKwic = item.get('kwic').replace('help', "<i><a href='" + wikiDonate + "'> help <a/></i>" )
                else:
                    newKwic = item.get('kwic')
                articleObj = {'title': item.get('title'),'url': item.get('url'),'kwic': newKwic}
                results[results.index(item)] = articleObj    
            
            images = getWikimages(keyword)
            wikiDef = None
                
            render = web.template.render('templates/')
            return render.results(results,keyword,wikiDef,images)
        except Exception:
            render = web.template.render('templates/')
            return render.error()
        
if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()